<template>
  <div class="login-container">
    <!-- <el-alert
      v-if="nodeEnv !== 'development'"
      title="七联一动"
      type="success"
      :closable="false"
      style="position: fixed"
    ></el-alert> -->
    <el-row>
      <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
        <div style="color: transparent">占位符</div>
      </el-col>
      <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
        <div class="form-box">
          <div class="title-tips">欢迎来到{{ title }}！</div>
          <el-form
            ref="form"
            :model="form"
            :rules="rules"
            class="login-form"
            label-position="left"
          >
            <el-form-item prop="mobile">
              <span class="svg-container svg-container-admin">
                <vab-icon :icon="['fas', 'user']" />
              </span>
              <el-input
                v-model.trim="form.mobile"
                v-focus
                placeholder="请输入用户名"
                tabindex="1"
                type="text"
              />
            </el-form-item>
            <el-form-item prop="password">
              <span class="svg-container">
                <vab-icon :icon="['fas', 'lock']" />
              </span>
              <el-input
                :key="passwordType"
                ref="password"
                v-model.trim="form.password"
                :type="passwordType"
                tabindex="2"
                placeholder="请输入密码"
                @keyup.enter.native="handleLogin"
              />
              <span
                v-if="passwordType === 'password'"
                class="show-password"
                @click="handlePassword"
              >
                <vab-icon :icon="['fas', 'eye-slash']"></vab-icon>
              </span>
              <span v-else class="show-password" @click="handlePassword">
                <vab-icon :icon="['fas', 'eye']"></vab-icon>
              </span>
            </el-form-item>
            <el-button
              :loading="loading"
              class="login-btn"
              type="primary"
              @click="handleLogin"
            >
              登录
            </el-button>
            <!-- <router-link to="/register">
            <div style="margin-top: 20px;">注册</div>
          </router-link> -->
          </el-form>
        </div>
      </el-col>
    </el-row>
    <div class="login-beian">
      <a href="http://beian.miit.gov.cn/" target="_blank">鄂ICP备18005935号</a>
    </div>
  </div>
</template>

<script>
  import { isPassword } from "@/utils/validate";

  export default {
    name: "Login",
    directives: {
      focus: {
        inserted(el) {
          el.querySelector("input").focus();
        },
      },
    },
    data() {
      const validateusername = (rule, value, callback) => {
        if ("" == value) {
          callback(new Error("用户名不能为空"));
        } else {
          callback();
        }
      };
      const validatePassword = (rule, value, callback) => {
        if (!isPassword(value)) {
          callback(new Error("密码不能少于6位"));
        } else {
          callback();
        }
      };
      return {
        nodeEnv: process.env.NODE_ENV,
        title: this.$baseTitle,
        form: {
          username: "",
          password: "",
        },
        rules: {
          username: [
            {
              required: true,
              trigger: "blur",
              validator: validateusername,
            },
          ],
          password: [
            {
              required: true,
              trigger: "blur",
              validator: validatePassword,
            },
          ],
        },
        loading: false,
        passwordType: "password",
        redirect: undefined,
      };
    },
    watch: {
      $route: {
        handler(route) {
          // console.log(1231231, route);
          this.redirect = (route.query && route.query.redirect) || "/";
        },
        immediate: true,
      },
    },
    created() {
      document.body.style.overflow = "hidden";
    },
    beforeDestroy() {
      document.body.style.overflow = "auto";
    },
    mounted() {
      // this.form.mobile = "admin";
      // this.form.password = "a123456";
      // setTimeout(() => {
      //   this.handleLogin();
      // }, 3000);
    },
    methods: {
      handlePassword() {
        this.passwordType === "password"
          ? (this.passwordType = "")
          : (this.passwordType = "password");
        this.$nextTick(() => {
          this.$refs.password.focus();
        });
      },
      handleLogin() {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.loading = true;
            this.$store
              .dispatch("user/login", this.form)
              .then(() => {
                console.log(this.redirect, "==============");
                const routerPath =
                  this.redirect === "/404" || this.redirect === "/401"
                    ? "/"
                    : this.redirect;
                this.$router.push(routerPath).catch(() => {});
                this.loading = false;
              })
              .catch(() => {
                this.loading = false;
              });
          } else {
            return false;
          }
        });
      },
    },
  };
</script>

<style lang="scss" scoped>
  .login-container {
    height: 100vh;
    background: url("~@/assets/login_images/lbg.png") center center fixed
      no-repeat;
    background-size: cover;

    .title {
      font-size: 54px;
      font-weight: 500;
      color: rgba(14, 18, 26, 1);
    }

    .title-tips {
      margin-top: 29px;
      margin-bottom: 39px;
      font-size: 32px;
      font-weight: 400;
      color: #00c0ff;
      text-align: center;
      text-overflow: ellipsis;
      white-space: nowrap;
    }

    @media only screen and (max-width: 767px) {
      .title-tips {
        font-size: 18px;
      }
    }

    .login-btn {
      display: inherit;
      width: 100%;
      height: 52px;
      margin-top: 5px;
      font-size: 20px;
      border: 0;

      &:hover {
        opacity: 0.9;
      }
    }

    .form-box {
      margin: calc((100vh - 425px) / 2) 15% 10%;
    }

    .login-form {
      position: relative;
      box-sizing: border-box;
      width: 412px;
      max-width: 100%;
      height: 324px;
      padding: 48px 42px;
      margin: 0 auto;
      overflow: hidden;
      background: url("~@/assets/login_images/loginbg.png") center center
        no-repeat;
      background-size: cover;

      .forget-password {
        width: 100%;
        margin-top: 40px;
        text-align: left;

        .forget-pass {
          width: 129px;
          height: 19px;
          font-size: 20px;
          font-weight: 400;
          color: rgba(92, 102, 240, 1);
        }
      }
    }

    .tips {
      margin-bottom: 10px;
      font-size: $base-font-size-default;
      color: $base-color-white;

      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }

    .title-container {
      position: relative;

      .title {
        margin: 0 auto 40px auto;
        font-size: 34px;
        font-weight: bold;
        color: $base-color-blue;
        text-align: center;
      }
    }

    .svg-container {
      position: absolute;
      top: 14px;
      left: 15px;
      z-index: $base-z-index;
      font-size: 16px;
      color: #d7dee3;
      cursor: pointer;
      user-select: none;
    }

    .show-password {
      position: absolute;
      top: 14px;
      right: 25px;
      font-size: 16px;
      color: #d7dee3;
      cursor: pointer;
      user-select: none;
    }

    ::v-deep {
      .el-form-item {
        padding-right: 0;
        // margin: 20px 0;
        margin-bottom: 36px;
        color: #454545;
        background: transparent;
        border: 1px solid transparent;
        border-radius: 2px;

        &__content {
          min-height: $base-input-height;
          line-height: $base-input-height;
        }

        &__error {
          position: absolute;
          top: 100%;
          left: 18px;
          font-size: $base-font-size-small;
          line-height: 18px;
          color: $base-color-red;
        }
      }

      .el-input {
        box-sizing: border-box;

        input {
          height: 52px;
          padding-left: 45px;
          font-size: $base-font-size-default;
          line-height: 52px;
          color: $base-font-color;
          background: #f6f4fc;
          border: 0;
          caret-color: $base-font-color;
        }
      }
    }

    .login-beian{
      text-align: center;
      position: absolute;
      width: 100%;
      bottom: 20px;
      a{
        color: #928b8b;
        &:hover{
          color: #ffffff;
        }
      }
    }
  }
</style>
